//
//  CHCamera.swift
//  bridging_js
//
//  Created by cyptt on 2021/8/18.
//

import UIKit
import Photos
import AVFoundation
import AVKit
import MobileCoreServices

public class CHSysCamera: NSObject,UIImagePickerControllerDelegate,UINavigationControllerDelegate{

    var pickerVC:UIImagePickerController = UIImagePickerController.init()
    
    var currentController:UIViewController?

    var sucessClosureB:((_ image:UIImage,_ fileUrl:URL?) ->Void)?  //成功回调 照片
    var failClosureB:((_ failMsg:String) -> Void)? //失败回调
    
    var successVideoB:((_ filePath:String) ->Void)? //视频回调
    
    
    
    
    public static let shared = CHSysCamera()
    private override init() {
        super.init()
        config()
    }
    
    public override func copy() -> Any {
        return self // SingletonClass.shared
    }
    
    public override func mutableCopy() -> Any {
        return self // SingletonClass.shared
    }
    

    func config()  {
        self.pickerVC.delegate = self;
        
    }
    
    public func openAlert(controller:UIViewController ,title:String = "请选择拍照/选择相册" ,itemNameArrs:[String] = ["选择相册","相机"],successB:@escaping ((_ image:UIImage,_ fileUrl:URL?)->Void),failB:@escaping ((_ failmsg:String)->Void)){
        
        CHDefaultAlert.show(with: controller, title: "", message: title, arr: itemNameArrs) { action in
    
            if action.title == nil{
                failB("出错了")
                return
            }
            if action.title!.contains("相册"){
                self.openAblum(controller: controller, successB: successB, failB: failB);
            }else if action.title!.contains("相机") {
                self.openCamera(controller: controller, successB: successB, failB: failB);
            }
            
        }
    }
    /**
     打开相册
       @param isCropper 是否裁剪
     */
    public func openAblum(controller:UIViewController,isCropper:Bool = false,successB:@escaping ((_ image:UIImage,_ fileUrl:URL?)->Void),failB:@escaping ((_ failmsg:String)->Void)) {
        currentController = controller
        self.sucessClosureB = successB
        self.failClosureB = failB
        self.pickerVC.mediaTypes = [(kUTTypeImage as String)];
        self.pickerVC.sourceType = UIImagePickerController.SourceType.photoLibrary;
      
        controller.present(self.pickerVC, animated: true, completion: nil)
       
    }
    
    /**
     打开相机
     */
    public  func openCamera(controller:UIViewController ,successB:@escaping((_ image:UIImage,_ fileUrl:URL?)->Void),failB:@escaping ((_ failmsg:String)->Void)) {
        currentController = controller
        self.sucessClosureB = successB
        self.failClosureB = failB
        self.pickerVC.mediaTypes = [(kUTTypeImage as String)];
        self.pickerVC.sourceType = UIImagePickerController.SourceType.camera;
        controller.present(self.pickerVC, animated: true, completion: nil)
    }
   
    
    /**
       开始录制视频
     */
    public  func openVideo(controller:UIViewController,successB:@escaping((_ filePath:String) ->Void),failB:@escaping ((_ failmsg:String)->Void)){
        self.pickerVC.sourceType = UIImagePickerController.SourceType.camera;
        self.pickerVC.mediaTypes = [(kUTTypeMovie as String)];
        
        self.successVideoB = successB
        self.failClosureB = failB
        self.currentController = controller
        controller.present(self.pickerVC, animated: true, completion: nil)
    }
    
    //MARK:--------UIImagePickerControllerDelegate
    
    public  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        print("-------",info)
     
        var url:URL?
        let mediaType:String = info[UIImagePickerController.InfoKey.mediaType] as! String
        print("----mediaType",mediaType);
        if mediaType == "public.image" {
            var image:UIImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

            if #available(iOS 11.0, *) {
                url = info[UIImagePickerController.InfoKey.imageURL] as? URL
            } else {
                
            }
           
                
            image = image.fixOrientation()
            
            currentController?.dismiss(animated: true, completion: nil)
            
            if (self.sucessClosureB != nil) {
                self.sucessClosureB!(image,url)
            }
         
         
        }else if(mediaType == "public.movie"){
            
            
            let file:URL = info[UIImagePickerController.InfoKey.mediaURL] as! URL
            let filePath:String = file.absoluteString

            currentController?.dismiss(animated: true, completion: nil)
            
            if self.successVideoB != nil {
                self.successVideoB!(filePath)
            }
        }
        
     
    }
    public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        currentController?.dismiss(animated: true, completion: nil)
        if (self.failClosureB != nil){  self.failClosureB!("用户取消了!")}
    
    }
    
}
